package ch.uzh.ifi.seal.ssqa.model.diagram.edit.policies;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.naming.directory.InitialDirContext;

import org.eclipse.core.internal.resources.File;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.command.CommandParameter;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.DragDropEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.requests.DropObjectsRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants;
import org.eclipse.gmf.runtime.notation.View;

import ch.uzh.ifi.seal.ssqa.model.DiagramComponent;
import ch.uzh.ifi.seal.ssqa.model.ModelPackage;

public class SourceDragDropEditPolicy extends DragDropEditPolicy {

	public IGraphicalEditPart getGraphicalHost() {
		if (getHost() instanceof IGraphicalEditPart) {
			return (IGraphicalEditPart) getHost();
		}
		return null;
	}

	public Command getDropCommand(Request request) {
		if (RequestConstants.REQ_DROP_OBJECTS.equals(request.getType())) {
			return new DropSourceCommand((DropObjectsRequest) request, this.getHost());
		}
		return null;
	}

	public Command getCommand(Request request) {
		return this.getDropCommand(request);
	}
	
	public class DropSourceCommand extends Command {
		private org.eclipse.emf.common.command.Command internalCommand;
		private TransactionalEditingDomain editingDomain;
		
		public DropSourceCommand(DropObjectsRequest request, EditPart host){
			List<String> paths = new ArrayList<String>();
			for(String object : (List<String>)request.getObjects()){
				IPath location= Path.fromOSString(object);
				IProject project= null;
				boolean isProject = false;
				for(IProject proj : ResourcesPlugin.getWorkspace().getRoot().getProjects()){
					if(Arrays.equals(proj.getLocation().segments(),location.segments())){
						project = proj;
						isProject = true;
						break;
					}
				}
				if(isProject){
					paths.add(project.getFullPath().toString());
				} else {
					IFile f = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(location.makeRelative());
					if(f != null){
						paths.add(f.getProject().getFullPath().append(f.getProjectRelativePath()).toString());
					}
				}
			}
			
			this.editingDomain = ((IGraphicalEditPart)host).getEditingDomain();
			View view = (View)host.getModel();
			DiagramComponent element = (DiagramComponent)view.getElement();
			this.internalCommand = editingDomain.createCommand(AddCommand.class, 
					new CommandParameter(element, ModelPackage.eINSTANCE.getDiagramComponent_SourcePaths(), paths ));
		}
		
		public void execute(){
			CommandStack stack = this.editingDomain.getCommandStack();
			stack.execute(this.internalCommand);
		}
	}
}
